package com.example.feifei.newproject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.example.feifei.fragment.DownLoadFragment;
import com.example.feifei.fragment.GraffitiFragment;
import com.example.feifei.fragment.MineFragment;
import com.example.feifei.fragment.NearByFrgment;
import com.example.feifei.fragment.RXJavaFragment;
import com.example.feifei.fragment.SettingFragment;
import com.example.feifei.fragment.buy.ui.ShoppingFragment;
import com.example.feifei.view.MyScrollView;

/**
 * Created by feifei on 2017/11/20.
 */

public class CustomViewActivity extends FragmentActivity{
    private MyScrollView mRightScrollView;
    private Button mShowMenuBtn;
    private ListView mMenuList;
    private ArrayAdapter<String> mAdapter;
    private String[] menus = {"附近的人", "我的资料", "设置", "游戏(购物车)", "RXJava+Retrofit+OKHttp","线程下载","涂鸦功能"};

    //进行Frtagment管理
    FragmentManager fragmentManager;

    NearByFrgment nearByFrgment;

    DownLoadFragment downLoadFragment;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
//        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_view);
        fragmentManager = this.getSupportFragmentManager();
//        setContentView(new MyViewGroup(this));
        mRightScrollView = (MyScrollView) findViewById(R.id.rightscrollview);
        final View menu = getLayoutInflater().inflate(R.layout.rightscrollview_menu, null);
        final View primary = getLayoutInflater().inflate(R.layout.rightscrollview_primary, null);
        mMenuList = (ListView) menu.findViewById(R.id.list_right_menu);
        mShowMenuBtn = (Button) primary.findViewById(R.id.btn_showmenu);
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menus);
        mMenuList.setAdapter(mAdapter);

        replaceFragment(new NearByFrgment(),false);

        mShowMenuBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(mRightScrollView.isShowMenu()){
                    mRightScrollView.hideMenu();
                }else{
                    mRightScrollView.showMenu();
                }
            }
        });

        mRightScrollView.setOnMenuChangedListener(new MyScrollView.OnMenuChangedListener() {
            public void onChanged(boolean isShow) {
                if(isShow){
                    mShowMenuBtn.setText("隐藏菜单");
                }else{
                    mShowMenuBtn.setText("显示菜单");
                }
            }
        });
        mRightScrollView.setPrimary(primary);
        mRightScrollView.setMenu(menu);

        mMenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch(position){
                    case 0:
                        nearByFrgment = new NearByFrgment();
                        replaceFragment(nearByFrgment,false);
                        break;
                    case 1:
                        replaceFragment(new MineFragment(),false);
                        break;
                    case 2:
                        replaceFragment(new SettingFragment(),false);
                        break;
                    case 3:
                        replaceFragment(new ShoppingFragment(),false);
                        break;
                    case 4:
                        replaceFragment(new RXJavaFragment(),false);
                        break;
                    case 5:
                        downLoadFragment = new DownLoadFragment();
                        replaceFragment(downLoadFragment,false);
                        break;
                        //涂鸦
                    case 6:
                        replaceFragment(new GraffitiFragment(),false);
                        break;
                }
                mRightScrollView.hideMenu();
            }
        });
    }
    //切换Fragment
    private void replaceFragment(Fragment newFragment,boolean addToBack){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.content_view,newFragment,"currentFragment");
        transaction.show(newFragment);
        if(addToBack){
            transaction.addToBackStack("");
        }
        transaction.commitAllowingStateLoss();

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(null != downLoadFragment){
            downLoadFragment.onRequestPermissionsResult(requestCode,permissions,grantResults);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        nearByFrgment.onActivityResult(requestCode,resultCode,data);
    }
}
